package cn.com.zonesion.rfiddemo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.zonesion.rfiddemo.R;
import cn.com.zonesion.rfiddemo.base.BaseFragment;
import cn.com.zonesion.rfiddemo.fragment.aboutfragment.AboutFragment;
import cn.com.zonesion.rfiddemo.fragment.aboutfragment.ExplainFragment;

public class AboutMainActivity extends FragmentActivity {

    @BindView(R.id.rg_bottom_tag_about)
    RadioGroup rgBottomTag;
    /**
     * 装多个Fragment的实例集合
     */
    private ArrayList<BaseFragment> fragments;
    private int position = 0;
    /**
     * 缓存的Fragemnt或者上次显示的Fragment
     */
    private Fragment tempFragemnt;
    private String verName;
    private String v;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_main);
        ButterKnife.bind(this);
        initView();
        /**
         * 初始化Fragment
         */
        initFragment();
        //设置RadioGroup的监听
        initListener();
    }

    private void initView() {
       verName = getIntent().getStringExtra("verName");
       v= getIntent().getStringExtra("v");
    }


    /**
     * 添加的时候要按照顺序
     */
    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new AboutFragment(verName,v));
        fragments.add(new ExplainFragment());

    }
    private void initListener(){
        rgBottomTag.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case  R.id.btn_about://关于
                        position = 0;
                        break;
                    case  R.id.btn_explain://说明
                        position = 1;
                        break;
                    default:
                        position = 0;
                        break;

                }
                //根据位置取不同的Fragment
                BaseFragment baseFragment = getFragment(position);
                switchFragment(tempFragemnt, baseFragment);
            }
        });
        rgBottomTag.check(R.id.btn_about);
    }
    private BaseFragment getFragment(int position) {
        if (fragments != null && fragments.size() > 0) {
            BaseFragment baseFragment = fragments.get(position);
            return baseFragment;
        }
        return null;
    }
    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {
        if (tempFragemnt != nextFragment) {
            tempFragemnt = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                //判断nextFragment是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    //添加Fragment
                    transaction.add(R.id.content_fragment_about, nextFragment).commit();
                } else {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }
}
